/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import entity.News;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.NewsModel;

/**
 * 
 * @author xuanhung2401
 */
@SuppressWarnings("serial")
@WebServlet(name = "NewsDetail", urlPatterns = { "/NewsDetail" })
public class NewsDetail extends HttpServlet {
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-Control", "public, max-age=86400");
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, 1);
		response.setHeader("Expires", cal.getTime().toString());
		try {
			NewsModel newsModel = new NewsModel();
			String path = ((HttpServletRequest) request).getRequestURI();
			System.out.println(path);
			String[] splittedURI = path.split("/");
			String alias = splittedURI[splittedURI.length - 1];
			System.out.println(alias);
			News n = newsModel.findByAlias(alias.replaceAll(".html", ""));
			n.setCountView(n.getCountView() + 1);
			newsModel.save(n);
			request.setAttribute("detail", n);
			request.getRequestDispatcher("/detail.jsp").forward(request,
					response);
		} catch (Exception e) {
			System.out.println(e.toString());
			response.sendRedirect("/home");
		}

	}

}
